﻿using System;
using System.Data.OleDb;

namespace XD.Office
{
    /// <summary>
    /// Access数据库操作类
    /// </summary>
    public class AccessHelper:IDisposable
    {
        #region 字段

        private string connectionString = string.Empty;
        /// <summary>
        /// 数据库连接器
        /// </summary>
        private OleDbConnection _conn = null;

        /// <summary>
        /// Command对象，用于执行SQL语句
        /// </summary>
        private OleDbCommand _comm = null;

        /// <summary>
        /// 事务
        /// </summary>
        private OleDbTransaction _trans = null;

        #endregion

        #region 构造函数

        private AccessHelper()
        { }

        public AccessHelper(string filePath) 
        {
            connectionString = "Provider= Microsoft.Jet.OLEDB.4.0;Data source=" + filePath;
            _conn = new OleDbConnection(connectionString);
            _comm = _conn.CreateCommand();
        }

        #endregion

        #region 数据库操作

        /// <summary>
        /// 打开数据库连接
        /// </summary>
        /// <returns>是否连接成功</returns>
        public void Open()
        {
            try
            {
                _conn.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public void Close()
        {
            if (_conn != null)
            {
                _comm.Dispose();
                _conn.Close();
                _conn.Dispose();
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            this.Close();
        }

        #endregion
    }
}
